package com.winning.vsts.merge.service.configuration;

import com.winning.vsts.merge.service.constant.ApiUrlEnum;
import com.winning.vsts.merge.service.domain.VstsProject;
import com.winning.vsts.merge.service.domain.VstsUser;
import com.winning.vsts.merge.service.service.VstsUserService;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.context.named.NamedContextFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import java.util.Objects;

/**
 * vsts 上下文
 *
 * @author xcl <xcl@winning.com.cn>
 * @date 2021/11/17
 */
public class VstsContext extends NamedContextFactory<VstsSpecification> {

    public VstsContext() {
        super(VstsConfiguration.class, "vsts", "vsts");
    }


    public RestTemplate restTemplate(String name) {
        RestTemplate instance = getInstance(name, RestTemplate.class);
        if (Objects.nonNull(instance)) {
            return instance;
        }
        return createRestTemplate(name);
    }

    private RestTemplate createRestTemplate(String name) {
        VstsUser vstsUser = vstsUser(name);
        if (Objects.isNull(vstsUser)) {
            getContext(name).registerBean(RestTemplate.class, () -> null);
            return getInstance(name, RestTemplate.class);
        }
        RestTemplate build = new RestTemplateBuilder()
            .basicAuthentication(vstsUser.getUserCode(), vstsUser.getUserToken())
            .rootUri(ApiUrlEnum.ROOT_URL.getUrl())
            .build();
        build.setRequestFactory(new HttpComponentsClientHttpRequestFactory());
        getContext(name).registerBean(RestTemplate.class, () -> build);
        return getInstance(name, RestTemplate.class);
    }

    public VstsUser vstsUser(String name) {
        VstsUser instance = getInstance(name, VstsUser.class);
        if (Objects.nonNull(instance)) {
            return instance;
        }
        return createVstsUser(name);
    }

    public VstsUser vstsUser(VstsProject vstsProject) {
        /*直接审批
        VstsUser vstsUser = vstsUser(vstsProject.getProjectOwner());
        if (Objects.nonNull(vstsUser)) {
            return vstsUser;
        }*/
        return vstsUser(vstsProject.getProjectSoa());
    }

    private VstsUser createVstsUser(String name) {
        VstsUserService vstsUserService = getInstance(name, VstsUserService.class);
        VstsUser vstsUser = vstsUserService.getByUserName(name);
        getContext(name).registerBean(VstsUser.class, () -> vstsUser);
        return getInstance(name, VstsUser.class);
    }


    public RestTemplate restTemplate(VstsProject vstsProject) {
        //由于仓库负责人令牌有效期只有1年，故全部用一个人账号合并，避免合并不一直
        /*RestTemplate restTemplate = restTemplate(vstsProject.getProjectOwner());
        if (Objects.nonNull(restTemplate)) {
            return restTemplate;
        }*/
        return restTemplate(vstsProject.getProjectSoa());
    }

    public RestTemplate restTemplateSoa(VstsProject vstsProject) {
        return restTemplate(vstsProject.getProjectSoa());
    }
}
